// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        image: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.image.active = false

        this.gameScene = ck.getGameScene()

        this.gameScene.node.on(ck.Event.GameStateChanged, this.onGameStateChanged, this)
    },

    onGameStateChanged(){
        if (this.gameScene.data.state == ck.GameState.FindDoor) {
            this.node.getComponent(cc.Animation).play('door_appear')

            ck.delayCall(this.node, 0.1, ()=>{
                this.image.active = true
            })
        }
    },

    onCollisionEnter: function (playerCollider, self) {
        if (!this.canCollision)
            return

        if (this.gameScene.data.state == ck.GameState.FindDoor) {
            this.node.getComponent(cc.Animation).play('door_disappear')
            this.gameScene.data.setState(ck.GameState.TouchDoor)
        }
    },

    onAppearFinished(){
        this.canCollision = true
    },

    onDisappearFinished(){
        this.gameScene.data.setWin(true)
        this.gameScene.data.setState(ck.GameState.Finished)
        this.node.active = false
    },
});
